package com.qiwx.pigeon.activity.summary;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.CellInfo;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.bg.BaseCellBackgroundFormat;
import com.bin.david.form.data.style.FontStyle;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.Utils.ScreenUtils;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.table.TDLayEgg;
import com.qiwx.pigeon.data.table.TDShouJ;
import com.qiwx.pigeon.data.params.ChartParam;
import com.qiwx.pigeon.data.vo.CageProduceBean;
import com.qiwx.pigeon.data.vo.EggLoseBean;
import com.qiwx.pigeon.data.vo.LayEggAmountBean;
import com.qiwx.pigeon.data.vo.QueryFertilizedBean;
import com.qiwx.pigeon.data.vo.SummaryOther;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.presenter.QueryCagePresenter;
import com.qiwx.pigeon.viewinterface.IQueryCageView;
import com.qiwx.pigeon.widget.ArrowView;

import org.angmarch.views.NiceSpinner;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-06-30 18:32
 * 数据统计首页
 **/
public class SummaryHomeActivity extends BaseMVPActivity<IQueryCageView, QueryCagePresenter> implements IQueryCageView {
    ChartParam param;
    String startDate;
    String endDate;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.title_right)
    TextView titleRight;
    @BindView(R.id.llTitle)
    LinearLayout llTitle;
    @BindView(R.id.spinner_wh)
    NiceSpinner spinnerWh;
    @BindView(R.id.txt_start_date)
    TextView txtStartDate;
    @BindView(R.id.txt_end_date)
    TextView txtEndDate;
    @BindView(R.id.lin_query_tools_date)
    LinearLayout linQueryToolsDate;
    @BindView(R.id.btn_condition)
    Button btnCondition;
    @BindView(R.id.btn_query)
    Button btnQuery;
    @BindView(R.id.rel_query_tools_top)
    RelativeLayout relQueryToolsTop;
    public static int wh_id = 46;
    public static int wh_number = 0;
    public int f_id = 0;
    List<WareHouseBean> wareHouseBeans = new ArrayList<>();
    @BindView(R.id.txt_yd_bad)
    TextView txtYdBad;
    @BindView(R.id.txt_yd_breaken)
    TextView txtYdBreaken;
    @BindView(R.id.txt_yd_wd)
    TextView txtYdWd;
    @BindView(R.id.txt_cd_bad)
    TextView txtCdBad;
    @BindView(R.id.txt_cd_breaken)
    TextView txtCdBreaken;
    @BindView(R.id.txt_cd_wd)
    TextView txtCdWd;
    @BindView(R.id.txt_fd_bad)
    TextView txtFdBad;
    @BindView(R.id.txt_fd_breaken)
    TextView txtFdBreaken;
    @BindView(R.id.txt_fd_wd)
    TextView txtFdWd;
    @BindView(R.id.txt_cz_bad)
    TextView txtCzBad;
    @BindView(R.id.txt_cz_breaken)
    TextView txtCzBreaken;
    @BindView(R.id.txt_cz_wd)
    TextView txtCzWd;
    @BindView(R.id.txt_cunl)
    TextView txtCunl;
    @BindView(R.id.txt_death)
    TextView txtDeath;
    @BindView(R.id.txt_ck)
    TextView txtCk;
    @BindView(R.id.txt_chul)
    TextView txtChul;
    @BindView(R.id.table_shouj)
    SmartTable<TDShouJ> tbShouJ;
    @BindView(R.id.table_layegg)
    SmartTable<TDLayEgg> tbLayEgg;
    int screen_width = 1024;

    @Override
    public QueryCagePresenter createPresenter() {
        return new QueryCagePresenter(new WeakReference<>(this));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_summary_home;
    }

    @Override
    public void initAfterOnCreate() {
        titleContent.setText("生产统计");
        titleReturn.changeArrowLeft();
        btnCondition.setVisibility(View.GONE);
        param = new ChartParam();
        f_id = AppContext.getEmployBean(this).getF_id();

        long start = DateTimeUtils.addDay(new Date(), -30).getTime();
        long end = DateTimeUtils.getCurrentStamp();
        txtStartDate.setText(DateTimeUtils.timeStamp2Date(start));
        txtEndDate.setText(DateTimeUtils.timeStamp2Date(end));
        startDate = txtStartDate.getText().toString();
        endDate = txtEndDate.getText().toString();
        screen_width = ScreenUtils.getScreenWidth(this);
        initSpinner(AppContext.houses);
        loadData();
    }

    //添加一种全部
    public void addAllWareHouse(List<WareHouseBean> result) {
        WareHouseBean wb = new WareHouseBean();
        wb.setId(-1);
        wb.setName("全部");
        result.add(wb);
    }

    public void initSpinner(List<WareHouseBean> data) {

        wareHouseBeans.addAll(data);
        //添加全部
        addAllWareHouse(wareHouseBeans);
        List<String> houses = new ArrayList<>();
        for (int i = 0; i < wareHouseBeans.size(); i++) {
            houses.add(wareHouseBeans.get(i).getName());
        }
        spinnerWh.attachDataSource(houses);
        spinnerWh.setOnSpinnerItemSelectedListener((parent, view, position, id) -> {
            wh_id = wareHouseBeans.get(position).getId();
            wh_number = wareHouseBeans.get(position).getNumber();
            loadData();
        });
        if (wareHouseBeans.size() > 0) {
            spinnerWh.setSelectedIndex(0);
            wh_id = wareHouseBeans.get(0).getId();
            wh_number = wareHouseBeans.get(0).getNumber();
        }

    }

    private void loadData() {
        param.setWh_id(wh_id);
        if (wh_id > 0) {
            param.setF_id(0);
        } else {
            param.setF_id(f_id);
        }
        param.setStart_date(startDate);
        param.setEnd_date(endDate);
        mPresenter.queryLayEggAmount(param);
        mPresenter.queryEggLoseAmount(param);
        mPresenter.queryShoujAmount(param);
        mPresenter.queryOtherAmount(param);
    }

    @Override
    public void onSearchSuccess(CageProduceBean bean) {

    }

    @Override
    public void onQueryCageSuccess(PageInfo<CageProduceBean> bean) {

    }

    @Override
    public void onQueryLayEggSuccess(List<LayEggAmountBean> data) {
        int amountAll = 0;
        int amountWo = 0;
        int amountOneW = 0;
        int amountTwoW = 0;
        for (int i = 0; i < data.size(); i++) {
            amountAll += data.get(i).getAmountAll();
            amountWo += data.get(i).getAmountWo();
            amountOneW += data.get(i).getAmountOneW();
            amountTwoW += data.get(i).getAmountTwoW();
        }
        initTDConfig(tbLayEgg);
        List<TDLayEgg> data1 = new ArrayList<>();
        TDLayEgg t1 = new TDLayEgg();
        t1.setCloumSort("2个");
        t1.setAmountD((amountTwoW * 2) + "");
        t1.setAmountW(amountTwoW + "");
        data1.add(t1);
        TDLayEgg t2 = new TDLayEgg();
        t2.setCloumSort("1个");
        t2.setAmountD(amountOneW + "");
        t2.setAmountW(amountOneW + "");
        data1.add(t2);
        TDLayEgg t4 = new TDLayEgg();
        t4.setCloumSort("其他");
        int otherD = amountAll - amountTwoW * 2 - amountOneW;
        int otherW = amountWo - amountTwoW - amountOneW;
        t4.setAmountD(otherD == 0 ? "--" : String.valueOf(otherD));
        t4.setAmountW(otherW == 0 ? "--" : String.valueOf(otherW));
        data1.add(t4);
        TDLayEgg t3 = new TDLayEgg();
        t3.setCloumSort("合计");
        t3.setAmountD(amountAll + "");
        t3.setAmountW(amountWo + "");
        data1.add(t3);

        tbLayEgg.setData(data1);

    }

    @Override
    public void onQueryEggLoseSuccess(List<EggLoseBean> data) {
        EggLoseBean bean = new EggLoseBean();
        for (int i = 0; i < data.size(); i++) {
            EggLoseBean lb = data.get(i);
            bean.setYd_bad(bean.getYd_bad() + lb.getYd_bad());
            bean.setYd_breaken(bean.getYd_breaken() + lb.getYd_breaken());
            bean.setYd_wd(bean.getYd_wd() + lb.getYd_wd());

            bean.setCd_bad(bean.getCd_bad() + lb.getCd_bad());
            bean.setCd_breaken(bean.getCd_breaken() + bean.getCd_breaken());
            bean.setCd_wd(bean.getCd_wd() + bean.getCd_wd());

            bean.setFd_bad(bean.getFd_bad() + lb.getFd_bad());
            bean.setFd_breaken(bean.getFd_breaken() + bean.getFd_breaken());
            bean.setFd_wd(bean.getFd_wd() + bean.getFd_wd());

            bean.setCz_bad(bean.getCz_bad() + lb.getCz_bad());
            bean.setCz_breaken(bean.getCz_breaken() + bean.getCz_breaken());
            bean.setCz_wdwz(bean.getCz_wdwz() + bean.getCz_wdwz());
        }
        txtYdBad.setText("坏蛋:" + bean.getYd_bad());
        txtYdBreaken.setText("破蛋:" + bean.getYd_breaken());
        txtYdWd.setText("无蛋:" + bean.getYd_wd());

        txtCdBad.setText("坏蛋:" + bean.getCd_bad());
        txtCdBreaken.setText("破蛋:" + bean.getCd_breaken());
        txtCdWd.setText("无蛋:" + bean.getCd_wd());

        txtFdBad.setText("坏蛋:" + bean.getFd_bad());
        txtFdBreaken.setText("破蛋:" + bean.getFd_breaken());
        txtFdWd.setText("无蛋:" + bean.getFd_wd());

        txtCzBad.setText("坏蛋:" + bean.getCz_bad());
        txtCzBreaken.setText("破蛋:" + bean.getCz_breaken());
        txtCzWd.setText("无蛋无崽:" + bean.getCz_wdwz());


    }

    @Override
    public void onQueryShoujSuccess(List<QueryFertilizedBean> data) {
        QueryFertilizedBean bean = new QueryFertilizedBean();
        for (int i = 0; i < data.size(); i++) {
            QueryFertilizedBean qb = data.get(i);
            bean.setShoujing2(bean.getShoujing2() + qb.getShoujing2());
            bean.setShoujing1(bean.getShoujing1() + qb.getShoujing1());

            bean.setWujing2(bean.getWujing2() + qb.getWujing2());
            bean.setWujing1(bean.getWujing1() + qb.getWujing1());

            bean.setSijing2(bean.getSijing2() + qb.getSijing2());
            bean.setSijing1(bean.getSijing1() + qb.getSijing1());

            bean.setShoujing2sijing1(bean.getShoujing2sijing1() + qb.getShoujing2sijing1());
            bean.setShoujing2wujing1(bean.getShoujing2wujing1() + qb.getShoujing2wujing1());
            bean.setWujing2sijing1(bean.getWujing2sijing1() + qb.getWujing2sijing1());

        }
//        txtShouj2.setText("2受精(窝):" + bean.getShoujing2());
//        txtWuj2.setText("2无精(窝):" + bean.getWujing2());
//        txtSij2.setText("2死精(窝):" + bean.getSijing2());
//
//        txtShouj1.setText("1受精(窝):" + bean.getShoujing1());
//        txtWuj1.setText("1无精(窝):" + bean.getWujing1());
//        txtSij1.setText("1死精(窝):" + bean.getSijing1());
//
//        txtShouj2si.setText("1受1死(窝):" + bean.getShoujing2sijing1());
//        txtShouj2wu.setText("1受1无(窝):" + bean.getShoujing2wujing1());
//        txtWu2si.setText("1无1死(窝):" + bean.getWujing2sijing1());
        initTDConfig(tbShouJ);
        List<TDShouJ> data1 = new ArrayList<>();
        //二个
        TDShouJ t1 = new TDShouJ();
        t1.setCloumSort("2个");
        t1.setShouj(bean.getShoujing2() + "");
        t1.setSij(bean.getSijing2() + "");
        t1.setWuj(bean.getWujing2() + "");
        data1.add(t1);
        //一个
        TDShouJ t2 = new TDShouJ();
        t2.setCloumSort("1个");
        int shouj1 = bean.getShoujing1() + bean.getShoujing2sijing1() + bean.getShoujing2wujing1();
        t2.setShouj(shouj1 + "");
        int sij1 = bean.getSijing1() + bean.getWujing2sijing1() + bean.getShoujing2sijing1();
        t2.setSij(sij1 + "");
        int wuj1 = bean.getWujing1() + bean.getWujing2sijing1() + bean.getShoujing2wujing1();
        t2.setWuj(wuj1 + "");
        data1.add(t2);
        //合计
        TDShouJ t3 = new TDShouJ();
        t3.setCloumSort("合计");
        t3.setShouj(bean.getShoujing2() + shouj1 + "");
        t3.setSij(bean.getSijing2() + sij1 + "");
        t3.setWuj(bean.getWujing2() + wuj1 + "");
        data1.add(t3);


        tbShouJ.setData(data1);


    }

    private void initTDConfig(SmartTable st) {
        FontStyle style = new FontStyle();
        style.setTextSpSize(this, 14);
        style.setTextColor(getResources().getColor(R.color.text_color_black));
        st.getConfig()
                .setShowTableTitle(true)
                .setShowColumnTitle(true)
                .setShowXSequence(false)//不显示行号
                .setShowYSequence(false)
                .setMinTableWidth(screen_width)//设置表格最小宽度
                .setVerticalPadding(ScreenUtils.dip2px(this, 2))
                .setContentStyle(style)
                .setTableTitleStyle(style)
                .setContentCellBackgroundFormat(new BaseCellBackgroundFormat<CellInfo>() {
                    @Override
                    public int getBackGroundColor(CellInfo cellInfo) {
                        if (cellInfo.row % 2 == 1) {
                            return ContextCompat.getColor(SummaryHomeActivity.this, R.color.content_bg);
                        }
                        return TableConfig.INVALID_COLOR;
                    }

                }).setColumnCellBackgroundFormat(new BaseCellBackgroundFormat<Column>() {
            @Override
            public int getBackGroundColor(Column column) {
//                if("area".equals(column.getFieldName())) {
//                    return ContextCompat.getColor(SummaryHomeActivity.this,R.color.column_bg);
//                }
                return TableConfig.INVALID_COLOR;
            }

            @Override
            public int getTextColor(Column column) {
//                if("area".equals(column.getFieldName())) {
//                    return ContextCompat.getColor(SummaryHomeActivity.this, R.color.white);
//                }else{
//                    return TableConfig.INVALID_COLOR;
//                }
                return TableConfig.INVALID_COLOR;

            }
        });

    }


    @Override
    public void onQueryOtherSuccess(List<SummaryOther> data) {
        SummaryOther other = new SummaryOther();
        for (int i = 0; i < data.size(); i++) {
            SummaryOther b = data.get(i);
            other.setCunlan(other.getCunlan() + b.getCunlan());
            other.setChulan(other.getChulan() + b.getChulan());
            other.setCkAmount(other.getCkAmount() + b.getCkAmount());
            other.setDeathAmount(other.getDeathAmount() + b.getDeathAmount());
        }
        txtCunl.setText("存栏(对):" + other.getCunlan());
        txtChul.setText("出栏(只):" + other.getChulan());
        txtCk.setText("出壳(只):" + other.getCkAmount());
        txtDeath.setText("鸽子死亡(只):" + other.getDeathAmount());

    }

    @Override
    public void onAddCageEliminateSuccess(int type) {

    }

    @Override
    public void onFailure(String error) {


    }

    @Override
    protected void onSelectDate(int type, String content) {
        if (type == 0) {
            startDate = content;
            txtStartDate.setText(startDate);
        } else {
            endDate = content;
            txtEndDate.setText(endDate);
        }
    }

    @OnClick({R.id.title_return, R.id.txt_start_date, R.id.txt_end_date, R.id.btn_query})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
                break;
            case R.id.txt_start_date:
                selectStartDate();
                break;
            case R.id.txt_end_date:
                selectDate(endDate, 1);
                break;
            case R.id.btn_query:
                loadData();
                break;
        }
    }

}
